package com.yoomiito.app.ui.anyview.income

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.IncomeTypeData
import com.yoomiito.app.widget.TipPop

class IncomeTypeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {


    fun intiView(types:List<IncomeTypeData>){
        if (childCount==0){
            types.forEach {
                val view = LayoutInflater.from(context).inflate(R.layout.view_income_type,null,false)
                view.findViewById<TextView>(R.id.tv_right).text = it.name
                view.findViewById<TextView>(R.id.tv_right_data).text = it.value
                view.findViewById<ImageView>(R.id.smallTip1).let {tip->
                    tip.visibility = if (it.tip.isEmpty()) View.GONE else View.VISIBLE
                    tip.setOnClickListener {view->
                        TipPop(context,view,it.tip)
                    }
                }
                val lp = LayoutParams(0,LayoutParams.MATCH_PARENT,1f)
                addView(view,lp)
            }
        }
    }

    fun updateData(types:List<IncomeTypeData>){
        for (i in 0 until childCount){
            val view = getChildAt(i)
//            view.findViewById<TextView>(R.id.tv_right).text = types[i].name
            view.findViewById<TextView>(R.id.tv_right_data).text = types[i].value
        }
    }
}